import glob


def read_one_xyz(filename):
    xyz = []
    with open(filename, 'r') as f:
        content = f.read()
        contact = content.split('\n')
        for line in contact:
            if line == '' or line.isdigit() or line.startswith("isomer"):
                continue
            else:
                atom = line.split()
                if len(atom) > 1:
                    xyzitem = {'symbol': atom[0], 'position': {"x": atom[1], "y": atom[2], "z": atom[3]}}
                    xyz.append(xyzitem)
    return xyz


XL = []
YL = []
ZL = []


def getUbAndLb(path="Au20_OPT_1000"):
    file_names = glob.glob(pathname='../data_process/data/' + path + '/*.xyz')
    for file in file_names:
        xyz = read_one_xyz(file)
        for i in xyz:
            XL.append(float(i['position']['x']))
            YL.append(float(i['position']['y']))
            ZL.append(float(i['position']['z']))
        print("*" * 20 + f'已完成对文件{str(file)}的读取' + '*' * 20)
    return [max(XL), max(YL), max(ZL)], [min(XL), min(YL), min(ZL)]


if __name__ == '__main__':
    ub, lb = getUbAndLb()
    print(ub, lb)
    [x_max, y_max, z_max], [x_min, y_min, z_min] = ub, lb
    print("*" * 60)
    print(f'For X, The upper bound is {x_max} and the lower bound is {x_min}')
    print("*" * 60)
    print(f'For Y, The upper bound is {y_max} and the lower bound is {y_min}')
    print("*" * 60)
    print(f'For Z, The upper bound is {z_max} and the lower bound is {z_min}')
